package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.AdminServiceImpl;
import com.qf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/27 19:09
 */

@WebServlet("/admin")
public class AdminController extends  BaseServlet {

    /**
     * 管理员登录
     * @param request
     * @param response
     * @return
     */
    public String login(HttpServletRequest request , HttpServletResponse response){
        //1、获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2、校验用户名、密码是否为空
        if (StrUtils.empty(username) || StrUtils.empty(password)){
            //为空
            request.setAttribute("msg", "请输入用户名、密码");
            return "redirct" + request.getContextPath() + "/login.jsp";
        }
        //3、调用service业务方法，校验用户名、密码是否正确
        AdminService adminService = new AdminServiceImpl();
        User user = adminService.login(username , password);
        //4、判断user是否为空
        if (user != null) {
            //5、将登陆成功后的用户放入tomcat的session会话中一直携带
            request.getSession().setAttribute("admin", user);
            //6、跳转到管理员首页
            return "redirect:" + request.getContextPath() + "/admin.jsp";
        }else {
            //用户名密码不正确、跳转到登录界面，重新登录
            return "redirct:"+request.getContextPath()+"/login.jsp";
        }
    }


}
